
layui.define(["table", "form"], function(e) {
    //表格内
    var admin = layui.admin;
    var $ = layui.$,
        form = layui.form,
        table = layui.table;
    //监听搜索
    form.on('submit(LAY-search)', function(data) {
        var field = data.field;
        //执行重载
        table.reload('LAY-admin-manage', {
            where: field
        });
    });
    //事件
    var active = {
        batchdel: function() {
            var checkStatus = table.checkStatus('LAY-admin-manage'),
                checkData = checkStatus.data; //得到选中的数据
            if (checkData.length === 0) {
                return layer.msg('请选择数据');
            }
            var reqBody = checkData.reduce(function(acc, cur) {           
                acc += cur.admin_id + ",";
                return acc;
            }, "");
            reqBody = reqBody.substr(0, reqBody.length - 1);           
            layer.confirm('确定删除吗？', function(index) {
                //执行 Ajax 后重载
                $.delete(layui.setter.app_url + 'admins/delete', {
                    id: reqBody
                }, function(data) {
                    layer.msg(data.msg);
                    table.reload('LAY-admin-manage');
                });
            });
        },
        add: function() {
            layer.open({
                type: 2,
                title: '添加管理员',
                content: layui.setter.app_url + 'admins/create',
                area: ['420px', '420px'],
                btn: ['确定', '取消'],
                yes: function(index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index],
                        submitID = 'LAY-submit',
                        submit = layero.find('iframe').contents().find('#' + submitID);
                    //监听提交
                    iframeWindow.layui.form.on('submit(' + submitID + ')', function(data) {
                        var field = data.field; //获取提交的字段              
                        $.post(layui.setter.app_url + 'admins/save', field, function(data, textStatus, xhr) {
                            layer.msg(data.msg);
                            layer.close(index); //关闭弹层
                            table.reload('LAY-admin-manage');
                        });
                    });
                    submit.trigger('click');
                }
            });
        }
    }
    $('.layui-btn.layuiadmin-btn-admin').on('click', function() {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
    table.render({
        elem: "#LAY-admin-manage",
        url: layui.setter.app_url + "admins/listData",
        cols: [
            [{
                type: "checkbox",
                fixed: "left"
            }, {
                field: "u_id",
                title: "ID",
                sort: !0
            }, {
                field: "username",
                title: "登录名"
            }, {
                field: "truename",
                title: "姓名"
            }, {
                field: "phone",
                title: "手机"
            }, {
                field: "email",
                title: "邮箱"
            }, {
                field: "r_names",
                title: "角色"
            }, {
                title: "操作",
                align: "center",
                fixed: "right",
                toolbar: "#table-tool"
            }]
        ],
        text: "对不起，加载出现异常！",
        page: !0,
        limit: 10,
        height: "full-220",
    }), table.on("tool(LAY-admin-manage)", function(e) {
        e.data;
        if ("del" === e.event) {
            layer.confirm("真的删除行么", function(t) {
                $.delete(layui.setter.app_url + 'admins/delete?id=' + e.data.admin_id, function(data) {
                    layer.msg(data.msg), e.del(), layer.close(t);
                });
            })
        }
    }), e("admins", {})
});